#!/usr/bin/env python
# -*- coding: utf-8 -*-
#


import os,os.path,tempfile,time

import celtuce.help.logging as logging

from celtuce.cell.bsd64db import Bsd64DB as CellDB

PATH = os.sep.join((tempfile.gettempdir(), 'celtuce/celldb'))


class   TestStorer_Hello(object) :
    def setUp(self) :
        logger = logging.getLogger()
        #logger.setLevel(logging.LEVEL_DEBUG)

        if os.path.exists(PATH) :
           self.removedirs(PATH)

        os.makedirs(PATH, 0777)

        self.storer = CellDB( PATH )

        ## setUp()

    def tearDown(self) :
        print 'shutdown storer ...'
        self.storer.shutdown()
        ## tearDown()

    def removedirs(self, path) :
        items = os.listdir(path)
        for name in items :
            fullpath = os.sep.join((path, name))
            if os.path.isdir(fullpath) :
                self.removedirs(fullpath)
            else :
                os.remove(fullpath)

        os.rmdir(path)
        ## removedirs

    def run(self) :
        self.testOne()
        self.testTwo()
        ## run()

    def testOne(self) :
        print 'test simple insert and find operation ...'
        key = 'hello'
        value = 'world'
        response = self.storer.insert( key, value, 12345678 )
        if not response :
           raise RuntimeError, 'fail to insert(%s, %s)' % (key, value)
        
        response = self.storer.find( key )
        if not response :
           raise RuntimeError, 'fail to find(key=%s)' % key
        print repr(response)
        assert response[3] == value
        print 'test simple insert and find operation done.'
        ## testOne()

    def testTwo(self) :
        print 'test insert performance ...'
        RECORD_COUNT = 10000
        key_prefix = 'hello'
        value_prefix = 'world'
        time_start = time.time()
        count = 0
        for i in range(RECORD_COUNT) :
            key = '-'.join((key_prefix, str(i)))
            value = '-'.join((value_prefix, str(i)))
            response = self.storer.insert( key, value, 12345678 )
            if not response :
               raise RuntimeError, 'fail to insert(%s, %s)' % (key, value)
            count += 1
        time_last = time.time() - time_start
        tps = count / time_last
        print ' - insert',count,'records last','%.2f' % time_last,'seconds, tps=','%.2f' % tps

        print 'test find performance ...'
        time_start = time.time()
        count = 0
        for i in range(RECORD_COUNT) :
            key = '-'.join((key_prefix, str(i)))
            value = '-'.join((value_prefix, str(i)))
            response = self.storer.find( key )
            if not response :
               raise RuntimeError, 'fail to find(key=%s)' % key
            #print repr(response)
            assert response[3] == value
            count += 1
        time_last = time.time() - time_start
        tps = count / time_last
        print ' - find',count,'records last','%.2f' % time_last,'seconds, tps=','%.2f' % tps
        ## testTwo()

    ## class TestStorer_Hello


if  __name__ == '__main__' :
    t = TestStorer_Hello()
    t.setUp()
    try :
        t.run()
    finally :
        t.tearDown()


